<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
use Admin\Controller\PublicController;
class OldstudentadminController extends AdminbaseController {
	
	protected $m;
	protected $yct;
	protected $districtService;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclOldstudent');
	}
	
    public function index(){
    	$this->display();
    }
    
    public function edit(){
    	$id = I('get.id');
    	if($id){
    		$map['id'] = $id;
    		$this->info = $this->m->where($map)->find();
    	}
    	$this->xiaoqu = D('YgclDistrict')->where(array('status'=>1))->order('listorder desc')->select();
    	$this->display();
    }
    
    //判断校区，课程，班级三者联动是否正常
    public function check_district_class_lesson($did,$cid,$lid){
    	$class = M('YgclClass')->where(array('did'=>$did,'id'=>$cid))->find();
    	if(!$class){
    		return false;
    	}
    	$lesson = M('YgclLesson')->where(array('cid'=>$cid,'id'=>$lid))->find();
    	if(!$lesson){
    		return false;
    	}
    	return true;
    }
    
    public function edit_post(){
    	if(IS_POST){
    		$checkDCL = $this->check_district_class_lesson($_POST['did'], $_POST['cid'], $_POST['lid']);
    		if(!$checkDCL){
    			$this->error("校区、课程、班级联动出错，请重新选择");
    		}
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		if($_POST['id']){
    			if($this->m->create()){
    				if($this->m->save()){
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$_POST['create_time'] = $_POST['update_time'];
    			if($this->m->create()){
    				$id = $this->m->add();
    				if($id){
    					$this->success('添加成功');
    				}else{
    					$this->error('添加失败');
    				}
    			}
    		}
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		$this->success('删除成功',U('index'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function import(){
    	$this->display();
    }
    public function import_post(){
    	if(IS_POST){
    		header("Content-type: text/html; charset=utf-8");
    		$config = array(
    				'fields' => array('student','tel','jzxm','xiaoqu','lid')
    		);
    		$excel = new \Common\ORG\MiniwebExcel($config);
    		$datas = $excel->import('.'.$_POST['url']);
    		$m = D('YgclOldstudent');
    		if($datas){
    			foreach ($datas as $dt){
    				$arr = R('Api/Lessonapi/_get_did_cid_lid',array($dt['lid']));
    				$d = array(
    					'student'=> $dt['student'],
    					'tel' => $dt['tel'],
    					'jzxm' => $dt['jzxm'],
    					'did'=> $arr['did'],
    					'cid'=> $arr['cid'],
    					'lid' => $arr['lid'],
    					'create_time' => Date('Y-m-d H:i:s'),
    					'update_time' => Date('Y-m-d H:i:s')
    				);
    				$m->add($d);
    			}
    		}
    		$this->success('导入成功');
    		//$data = D('StudybookChildren')->where(array('pid'=>3))->field('id,name,update_time')->select();
    		 
    		//$excel->export($data);
    	}
    }
}